home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 050 / tpstuff1.arc / KNIGHT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-07-17  |  27.6 KB  |  763 lines

  1. {$C+}
  2. PROGRAM game(INPUT,OUTPUT);
  3.   VAR
  4.     i_move_first             : BOOLEAN;
  5.     counter_0,
  6.     counter_1,
  7.     counter_2,
  8.     counter_3,
  9.     counter_4,
  10.     counter_5,
  11.     counter_6, 
  12.     counter_7,
  13.     level_of_difficulty,
  14.     max_plys,
  15.     my_score,
  16.     plys_left,
  17.     tiles_visited,
  18.     your_moves_left,
  19.     your_score               : INTEGER;
  20.     board                    : ARRAY [0..9,0..4,0..4] OF INTEGER;
  21.     my_column,
  22.     my_row,
  23.     your_column,
  24.     your_row                 : ARRAY [0..9] OF INTEGER;
  25.     column_change,
  26.     row_change               : ARRAY [1..8] OF INTEGER;
  27.   PROCEDURE display_rules;
  28.     BEGIN
  29.       WRITELN(OUTPUT,
  30.        '     Square tiles cover a board five tiles high and five tiles');
  31.       WRITELN(OUTPUT,
  32.        'wide.  You start on the lower left tile and I start on the upper');
  33.       WRITELN(OUTPUT,
  34.        'right tile.  We take turns moving like a knight in a chess game.');
  35.       WRITELN(OUTPUT,
  36.        'Neither of us may move off the board.  Each tile that has not yet');
  37.       WRITELN(OUTPUT,
  38.        'been occupied has a number of dollars on it.  As one moves onto a');
  39.       WRITELN(OUTPUT,
  40.        'tile, one collects the dollars on that tile.  We each get fifteen');
  41.       WRITELN(OUTPUT,
  42.        'moves.  Whoever collects more dollars wins.');
  43.     END;
  44.   FUNCTION you_move_first : BOOLEAN;
  45.     VAR
  46.       response_okay                  : BOOLEAN;
  47.       response                       : CHAR;
  48.     BEGIN
  49.       WRITELN(OUTPUT,' ');
  50.       REPEAT
  51.         WRITE(OUTPUT,'     Do you want to move first or second?  ');
  52.         READLN(INPUT,response);
  53.         IF ((response = 'f') OR (response = 'F')) THEN
  54.           BEGIN
  55.             response_okay:=TRUE;
  56.             you_move_first:=TRUE
  57.           END
  58.         ELSE
  59.           IF ((response = 's') OR (response = 'S')) THEN
  60.             BEGIN
  61.               response_okay:=TRUE;
  62.               you_move_first:=FALSE
  63.             END
  64.           ELSE
  65.             response_okay:=FALSE
  66.       UNTIL
  67.         (response_okay)
  68.     END;
  69.   PROCEDURE get_level_of_difficulty;
  70.     BEGIN
  71.       WRITELN(OUTPUT,' ');
  72.       REPEAT
  73.         WRITE('     Level of difficulty (1-9)?  ');
  74.         READLN(INPUT,level_of_difficulty)
  75.       UNTIL
  76.         ((level_of_difficulty >= 1) AND (level_of_difficulty <= 9))
  77.     END;
  78.   PROCEDURE get_game_number;
  79.     VAR
  80.       char_index       : INTEGER;
  81.       game_number_okay : BOOLEAN;
  82.       game_number      : STRING[8];
  83.     BEGIN
  84.       WRITELN(OUTPUT,' ');
  85.       game_number_okay:=FALSE;
  86.       REPEAT
  87.         WRITE('     Eight digit game number?  ');
  88.         READLN(INPUT,game_number);
  89.         WHILE (LENGTH(game_number) < 8) DO
  90.           game_number:=CONCAT('0',game_number);
  91.         game_number_okay:=TRUE;
  92.         char_index:=1;
  93.         WHILE ((char_index <= 8) AND (game_number_okay)) DO
  94.           IF ((game_number[char_index] < '0')
  95.           OR  (game_number[char_index] > '9')) THEN
  96.             game_number_okay:=FALSE
  97.           ELSE
  98.             char_index:=char_index+1
  99.       UNTIL
  100.         (game_number_okay);
  101.       counter_0:=ORD(game_number[8])-ORD('0');
  102.       counter_1:=ORD(game_number[7])-ORD('0');
  103.       counter_2:=ORD(game_number[6])-ORD('0');
  104.       counter_3:=ORD(game_number[5])-ORD('0');
  105.       counter_4:=ORD(game_number[4])-ORD('0');
  106.       counter_5:=ORD(game_number[3])-ORD('0');
  107.       counter_6:=ORD(game_number[2])-ORD('0');
  108.       counter_7:=ORD(game_number[1])-ORD('0')
  109.     END;
  110.   FUNCTION uniform : REAL;
  111.     TYPE
  112.       substitution_array = ARRAY [0..99] OF INTEGER;
  113.     CONST
  114.       substitution_high : substitution_array =
  115.        (4,1,2,8,8,9,9,6,5,7,2,1,2,9,8,8,6,3,5,1,9,5,4,4,9,8,6,0,8,0, 
  116.         6,0,2,4,1,9,2,0,7,4,7,3,0,0,2,6,8,9,4,0,8,3,2,3,2,5,2,4,6,9, 
  117.         7,9,1,3,5,7,1,1,4,5,8,1,6,0,5,7,8,2,3,3,7,3,5,1,7,5,4,0,3,6, 
  118.         3,7,7,1,9,4,0,5,6,6);   
  119.       substitution_low : substitution_array =
  120.        (1,2,2,1,5,5,4,6,4,6,4,4,5,6,6,3,0,9,6,5,7,2,0,9,3,4,2,3,9,1, 
  121.         9,9,9,3,8,9,3,4,1,5,0,5,2,7,0,8,8,0,4,5,0,3,6,8,1,7,8,8,7,1, 
  122.         3,2,7,7,1,8,0,3,7,5,2,6,4,0,9,9,7,7,4,6,2,0,0,1,7,3,6,6,1,1, 
  123.         2,4,5,9,8,2,8,8,3,5);   
  124.     VAR
  125.       iteration,
  126.       seed_0,
  127.       seed_1,
  128.       seed_2,
  129.       seed_3,
  130.       seed_4,
  131.       seed_5,
  132.       seed_6,
  133.       seed_7,
  134.       substitution_index,
  135.       tem_0,             
  136.       tem_1,             
  137.       tem_3,             
  138.       tem_int 
  139.                                       : INTEGER;
  140.       dividend,
  141.       sum                             : REAL;
  142.     BEGIN
  143.       tem_int:=counter_0+1;   
  144.       IF tem_int <= 9 THEN   
  145.         counter_0:=tem_int
  146.       ELSE         
  147.         BEGIN       
  148.           counter_0:=0;   
  149.           tem_int:=counter_1+1;      
  150.           IF tem_int <= 9 THEN                                      
  151.             counter_1:=tem_int
  152.           ELSE                                                      
  153.             BEGIN
  154.               counter_1:=0;         
  155.               tem_int:=counter_2+1;                                  
  156.               IF tem_int <= 9 THEN                                  
  157.                 counter_2:=tem_int
  158.               ELSE           
  159.                 BEGIN
  160.                   counter_2:=0;   
  161.                   tem_int:=counter_3+1;
  162.                   IF tem_int <= 9 THEN                     
  163.                     counter_3:=tem_int
  164.                   ELSE   
  165.                     BEGIN
  166.                       counter_3:=0; 
  167.                       tem_int:=counter_4+1; 
  168.                       IF tem_int <= 9 THEN 
  169.                         counter_4:=tem_int
  170.                       ELSE 
  171.                         BEGIN
  172.                           counter_4:=0; 
  173.                           tem_int:=counter_5+1; 
  174.                           IF tem_int <= 9 THEN 
  175.                             counter_5:=tem_int
  176.                           ELSE 
  177.                             BEGIN
  178.                               counter_5:=0; 
  179.                               tem_int:=counter_6+1; 
  180.                               IF tem_int <= 9 THEN 
  181.                                 counter_6:=tem_int
  182.                               ELSE 
  183.                                 BEGIN
  184.                                   counter_6:=0; 
  185.                                   tem_int:=counter_7+1; 
  186.                                   IF tem_int <= 9 THEN 
  187.                                     counter_7:=tem_int
  188.                                   ELSE 
  189.                                     counter_7:=0
  190.                                 END
  191.                             END
  192.                         END
  193.                     END
  194.                 END
  195.             END
  196.         END;
  197.       seed_0:=counter_0;   
  198.       seed_1:=counter_1;        
  199.       seed_2:=counter_2;   
  200.       seed_3:=counter_3;   
  201.       seed_4:=counter_4;   
  202.       seed_5:=counter_5;   
  203.       seed_6:=counter_6;   
  204.       seed_7:=counter_7;   
  205.       FOR iteration:=1 TO 8 DO
  206.         BEGIN
  207.           substitution_index:=10*seed_1+seed_0;   
  208.           tem_0:=substitution_low[substitution_index];    
  209.           tem_1:=substitution_high[substitution_index];   
  210.           substitution_index:=10*seed_3+seed_2;           
  211.           seed_0:=substitution_low[substitution_index]; 
  212.           tem_3:=substitution_high[substitution_index]; 
  213.           substitution_index:=10*seed_5+seed_4; 
  214.           seed_2:=substitution_low[substitution_index]; 
  215.           seed_1:=substitution_high[substitution_index]; 
  216.           substitution_index:=10*seed_7+seed_6; 
  217.           seed_5:=substitution_low[substitution_index]; 
  218.           seed_7:=substitution_high[substitution_index]; 
  219.           seed_3:=tem_0; 
  220.           seed_6:=tem_1;   
  221.           seed_4:=tem_3
  222.         END;              
  223.       tem_int:=1000*seed_3+100*seed_2+10*seed_1+seed_0; 
  224.       sum:=tem_int;      
  225.       sum:=sum/10000.0;  
  226.       tem_int:=1000*seed_7+100*seed_6+10*seed_5+seed_4; 
  227.       dividend:=tem_int;
  228.       sum:=(dividend+sum)/10000.0;
  229.       uniform:=sum                          
  230.     END;
  231.   PROCEDURE set_up_board;
  232.     VAR
  233.       column,
  234.       row                          : INTEGER;
  235.     BEGIN
  236.       FOR row:=0 TO 4 DO
  237.         FOR column:=0 TO 4 DO
  238.           board[0,column,row]:=1+TRUNC(9*uniform);
  239.       board[0,0,0]:=0;
  240.       board[0,4,4]:=0
  241.     END;
  242.   PROCEDURE display_board;
  243.     VAR
  244.       column,
  245.       row                          : INTEGER;
  246.     BEGIN
  247.       WRITELN(OUTPUT,' ');
  248.       FOR row:=4 DOWNTO 0 DO
  249.         BEGIN
  250.           WRITE(OUTPUT,'         ');
  251.           FOR column:=0 TO 4 DO
  252.             IF ((column = my_column[0]) AND (row = my_row[0])) THEN
  253.               WRITE(OUTPUT,' +')
  254.             ELSE
  255.               IF ((column = your_column[0]) AND (row = your_row[0])) THEN
  256.                 WRITE(OUTPUT,' *')
  257.               ELSE
  258.                 WRITE(OUTPUT,board[0,column,row]:2);
  259.           WRITELN(OUTPUT,' ')
  260.         END;
  261.       WRITELN(OUTPUT,' ');
  262.       WRITE(OUTPUT,'     My winnings:  ');
  263.       WRITE(OUTPUT,my_score:3);
  264.       WRITE(OUTPUT,'     Your winnings:  ');
  265.       WRITELN(OUTPUT,your_score:3)
  266.     END;
  267.   FUNCTION on_board(VAR column,row : INTEGER) : BOOLEAN;
  268.     BEGIN
  269.       IF column < 0 THEN
  270.         on_board:=FALSE
  271.       ELSE
  272.         IF column > 4 THEN
  273.           on_board:=FALSE
  274.         ELSE
  275.           IF row < 0 THEN
  276.             on_board:=FALSE
  277.           ELSE
  278.             IF row > 4 THEN
  279.               on_board:=FALSE
  280.             ELSE
  281.               on_board:=TRUE
  282.     END;
  283.   PROCEDURE get_your_move;
  284.     VAR
  285.       change_index,
  286.       board_column,
  287.       board_row,
  288.       increment,
  289.       limit,
  290.       move_column,
  291.       move_row,
  292.       next_column,
  293.       next_row,
  294.       permissible_move_count,
  295.       permissible_move_index,
  296.       tem_1                            : INTEGER;
  297.       permissible_move                 : ARRAY [1..8] OF INTEGER;
  298.       move                             : ARRAY [1..8,-2..2,-2..2] OF INTEGER;
  299.     BEGIN
  300.       WRITELN(OUTPUT,' ');
  301.       WRITE(OUTPUT,'     You have ');
  302.       WRITE(OUTPUT,your_moves_left:2);
  303.       WRITELN(OUTPUT,' moves left.');
  304.       WRITELN(OUTPUT,' ');
  305.       WRITELN(OUTPUT,'     Permissible moves:');
  306.       permissible_move_count:=0;
  307.       FOR change_index:=1 TO 8 DO
  308.         BEGIN
  309.           next_column:=your_column[0]+column_change[change_index];
  310.           next_row:=your_row[0]+row_change[change_index];
  311.           IF on_board(next_column,next_row) THEN
  312.             BEGIN
  313.               permissible_move_count:=permissible_move_count+1;
  314.               permissible_move[permissible_move_count]:=change_index;
  315.               FOR move_column:=-2 TO 2 DO
  316.                 FOR move_row:=-2 TO 2 DO
  317.                   move[permissible_move_count,move_column,move_row]:=-2;
  318.               move[permissible_move_count,0,0]:=-1;
  319.               limit:=column_change[change_index];
  320.               IF limit > 0 THEN
  321.                 increment:=1
  322.               ELSE
  323.                 increment:=-1;
  324.               move_column:=0;
  325.               move_row:=0;
  326.               board_column:=your_column[0];
  327.               board_row:=your_row[0];
  328.               REPEAT
  329.                 move_column:=move_column+increment;
  330.                 board_column:=board_column+increment;
  331.                 move[permissible_move_count,move_column,move_row]
  332.                  :=board[0,board_column,board_row]
  333.               UNTIL
  334.                 (move_column = limit);
  335.               limit:=row_change[change_index];
  336.               IF limit > 0 THEN
  337.                 increment:=1
  338.               ELSE
  339.                 increment:=-1;
  340.               REPEAT
  341.                 move_row:=move_row+increment;
  342.                 board_row:=board_row+increment;
  343.                 move[permissible_move_count,move_column,move_row]
  344.                  :=board[0,board_column,board_row]
  345.               UNTIL
  346.                 (move_row = limit) 
  347.             END
  348.         END;
  349.       WRITELN(OUTPUT,' ');
  350.       FOR move_row:=2 DOWNTO -2 DO
  351.         BEGIN
  352.           WRITE(OUTPUT,'      ');
  353.           FOR permissible_move_index:=1 TO permissible_move_count DO
  354.             BEGIN
  355.               WRITE(OUTPUT,'   ');
  356.               FOR move_column:=-2 TO 2 DO
  357.                 BEGIN
  358.                   tem_1:=move[permissible_move_index,move_column,move_row];
  359.                   IF tem_1 < -1 THEN
  360.                     WRITE(OUTPUT,' ')
  361.                   ELSE
  362.                     IF tem_1 < 0 THEN
  363.                       WRITE(OUTPUT,'*')
  364.                     ELSE
  365.                       WRITE(OUTPUT,tem_1:1)
  366.                 END
  367.             END;
  368.           WRITELN(OUTPUT,' ') 
  369.         END;
  370.       WRITE(OUTPUT,'      ');
  371.       FOR permissible_move_index:=1 TO permissible_move_count DO
  372.         BEGIN
  373.           WRITE(OUTPUT,'  Move ');
  374.           WRITE(OUTPUT,permissible_move_index:1)
  375.         END;
  376.       WRITELN(OUTPUT,' ');
  377.       WRITELN(OUTPUT,' ');
  378.       REPEAT
  379.         WRITE(OUTPUT,'     Which move do you wish to make?  ');
  380.         READLN(INPUT,permissible_move_index) 
  381.       UNTIL
  382.         ((permissible_move_index >= 1)
  383.       AND (permissible_move_index <= permissible_move_count));
  384.       change_index:=permissible_move[permissible_move_index];
  385.       next_column:=your_column[0]+column_change[change_index];
  386.       next_row:=your_row[0]+row_change[change_index];
  387.       your_column[0]:=next_column;
  388.       your_row[0]:=next_row;
  389.       your_score:=your_score+board[0,next_column,next_row];
  390.       IF board[0,next_column,next_row] > 0 THEN
  391.         tiles_visited:=tiles_visited+1;
  392.       board[0,next_column,next_row]:=0;
  393.       your_moves_left:=your_moves_left-1;
  394.       plys_left:=plys_left-1
  395.     END;
  396.   PROCEDURE make_my_move;
  397.     VAR
  398.       finished,
  399.       skip_branch,
  400.       valid_move_found      : BOOLEAN;
  401.       change_index,
  402.       change_limit,
  403.       column,
  404.       final_change_index,
  405.       max_depth,
  406.       next_column,
  407.       next_row,
  408.       ply,
  409.       ply_minus_1,
  410.       ply_plus_1,
  411.       row,
  412.       tem_1,
  413.       tem_2                 : INTEGER;
  414.       branch                : ARRAY [1..9] OF INTEGER;
  415.       min_max               : ARRAY [1..9,1..8] OF INTEGER;
  416.       partial_sum           : ARRAY [0..9] OF INTEGER;
  417.     BEGIN
  418.       max_depth:=max_plys;
  419.       IF plys_left < max_depth THEN
  420.         max_depth:=plys_left;
  421.       partial_sum[0]:=my_score-your_score;
  422.       FOR ply:=1 TO max_depth DO
  423.         BEGIN
  424.           IF ODD(ply) THEN
  425.             FOR change_index:=1 TO 8 DO
  426.               min_max[ply,change_index]:=32000
  427.           ELSE
  428.             FOR change_index:=1 TO 8 DO
  429.               min_max[ply,change_index]:=-32000;
  430.           branch[ply]:=8;  
  431.         END;
  432.       branch[1]:=0;
  433.       ply:=max_depth;
  434.       REPEAT
  435.         finished:=FALSE;
  436.         WHILE ((ply >= 1) AND (NOT finished)) DO
  437.           BEGIN
  438.             branch[ply]:=branch[ply]+1;
  439.             IF branch[ply] > 8 THEN
  440.               BEGIN
  441.                 branch[ply]:=1;
  442.                 ply_plus_1:=ply;
  443.                 ply:=ply-1;
  444.                 ply_minus_1:=ply-1;
  445.                 IF ply > 0 THEN
  446.                   BEGIN
  447.                     IF ODD(ply) THEN
  448.                       BEGIN
  449.                         tem_2:=min_max[ply,branch[ply]];
  450.                         FOR change_index:=1 TO 8 DO
  451.                           BEGIN
  452.                             tem_1:=min_max[ply_plus_1,change_index];
  453.                             IF tem_1 > -32000 THEN
  454.                               BEGIN
  455.                                 IF tem_1 < tem_2 THEN tem_2:=tem_1;
  456.                                 min_max[ply_plus_1,change_index]:=-32000
  457.                               END
  458.                           END;
  459.                         min_max[ply,branch[ply]]:=tem_2;
  460.                         IF tem_2 < 32000 THEN
  461.                           BEGIN
  462.                             IF ply_minus_1 > 0 THEN
  463.                               BEGIN
  464.                                 change_index:=1;
  465.                                 change_limit:=branch[ply_minus_1];
  466.                                 skip_branch:=FALSE;
  467.                                 WHILE ((change_index < change_limit)
  468.                                 AND    (NOT skip_branch)) DO
  469.                                   BEGIN
  470.                                     tem_1:=min_max[ply_minus_1,change_index];
  471.                                     IF tem_1 > -32000 THEN
  472.                                       IF tem_2 >= tem_1 THEN
  473.                                         skip_branch:=TRUE
  474.                                       ELSE
  475.                                         change_index:=change_index+1
  476.                                     ELSE
  477.                                       change_index:=change_index+1
  478.                                   END;
  479.                                 IF skip_branch THEN
  480.                                   BEGIN
  481.                                     change_limit:=branch[ply];
  482.                                     FOR change_index:=1 TO change_limit DO
  483.                                       min_max[ply,change_index]:=32000;
  484.                                     branch[ply]:=1;
  485.                                     ply:=ply_minus_1
  486.                                   END
  487.                               END
  488.                           END
  489.                       END
  490.                     ELSE
  491.                       BEGIN
  492.                         tem_2:=min_max[ply,branch[ply]];
  493.                         FOR change_index:=1 TO 8 DO
  494.                           BEGIN
  495.                             tem_1:=min_max[ply_plus_1,change_index];
  496.                             IF tem_1 < 32000 THEN
  497.                               BEGIN
  498.                                 IF tem_1 > tem_2 THEN tem_2:=tem_1;
  499.                                 min_max[ply_plus_1,change_index]:=32000
  500.                               END
  501.                           END;
  502.                         min_max[ply,branch[ply]]:=tem_2;
  503.                         IF tem_2 > -32000 THEN
  504.                           BEGIN
  505.                             IF ply_minus_1 > 0 THEN
  506.                               BEGIN
  507.                                 change_index:=1;
  508.                                 change_limit:=branch[ply_minus_1];
  509.                                 skip_branch:=FALSE;
  510.                                 WHILE ((change_index < change_limit)
  511.                                 AND    (NOT skip_branch)) DO
  512.                                   BEGIN
  513.                                     tem_1:=min_max[ply_minus_1,change_index];
  514.                                     IF tem_1 < 32000 THEN
  515.                                       IF tem_2 <= tem_1 THEN
  516.                                         skip_branch:=TRUE
  517.                                       ELSE
  518.                                         change_index:=change_index+1
  519.                                     ELSE
  520.                                       change_index:=change_index+1
  521.                                   END;
  522.                                 IF skip_branch THEN
  523.                                   BEGIN
  524.                                     change_limit:=branch[ply];
  525.                                     FOR change_index:=1 TO change_limit DO
  526.                                       min_max[ply,change_index]:=-32000;
  527.                                     branch[ply]:=1;
  528.                                     ply:=ply_minus_1
  529.                                   END
  530.                               END
  531.                           END
  532.                       END
  533.                   END
  534.               END
  535.             ELSE
  536.               BEGIN
  537.                 change_index:=branch[ply];
  538.                 ply_minus_1:=ply-1;
  539.                 IF ODD(ply) THEN
  540.                   BEGIN
  541.                     next_column
  542.                      :=my_column[ply_minus_1]+column_change[change_index];
  543.                     next_row
  544.                      :=my_row[ply_minus_1]+row_change[change_index]
  545.                   END
  546.                 ELSE
  547.                   BEGIN
  548.                     next_column
  549.                      :=your_column[ply_minus_1]+column_change[change_index];
  550.                     next_row
  551.                      :=your_row[ply_minus_1]+row_change[change_index]
  552.                   END;
  553.                 IF on_board(next_column,next_row) THEN
  554.                   finished:=TRUE
  555.               END
  556.           END;
  557.         IF ply > 0 THEN
  558.           BEGIN
  559.             ply_minus_1:=ply-1;
  560.             WHILE (ply <= max_depth) DO
  561.               BEGIN
  562.                 change_index:=branch[ply];
  563.                 valid_move_found:=FALSE;
  564.                 IF ODD(ply) THEN
  565.                   WHILE (NOT valid_move_found) DO
  566.                     BEGIN
  567.                       next_column
  568.                        :=my_column[ply_minus_1]+column_change[change_index];
  569.                       next_row
  570.                        :=my_row[ply_minus_1]+row_change[change_index];
  571.                       IF on_board(next_column,next_row) THEN
  572.                         BEGIN
  573.                           valid_move_found:=TRUE;
  574.                           my_column[ply]:=next_column;
  575.                           my_row[ply]:=next_row;
  576.                           your_column[ply]:=your_column[ply_minus_1];
  577.                           your_row[ply]:=your_row[ply_minus_1];
  578.                           partial_sum[ply]
  579.                            :=partial_sum[ply_minus_1]
  580.                            +board[ply_minus_1,next_column,next_row]
  581.                         END
  582.                       ELSE
  583.                         change_index:=change_index+1
  584.                     END 
  585.                 ELSE
  586.                   WHILE (NOT valid_move_found) DO
  587.                     BEGIN
  588.                       next_column
  589.                        :=your_column[ply_minus_1]
  590.                        +column_change[change_index];
  591.                       next_row:=your_row[ply_minus_1]
  592.                        +row_change[change_index];
  593.                       IF on_board(next_column,next_row) THEN
  594.                         BEGIN
  595.                           valid_move_found:=TRUE;
  596.                           my_column[ply]:=my_column[ply_minus_1];
  597.                           my_row[ply]:=my_row[ply_minus_1];
  598.                           your_column[ply]:=next_column;
  599.                           your_row[ply]:=next_row;
  600.                           partial_sum[ply]
  601.                            :=partial_sum[ply_minus_1]
  602.                            -board[ply_minus_1,next_column,next_row]
  603.                         END
  604.                       ELSE
  605.                         change_index:=change_index+1
  606.                     END;
  607.                 branch[ply]:=change_index;
  608.                 FOR column:=0 TO 4 DO
  609.                   FOR row:=0 TO 4 DO
  610.                     board[ply,column,row]:=board[ply_minus_1,column,row];
  611.                 board[ply,next_column,next_row]:=0;
  612.                 ply:=ply+1;
  613.                 ply_minus_1:=ply_minus_1+1
  614.               END;
  615.             tem_2:=partial_sum[max_depth];
  616.             min_max[max_depth,branch[max_depth]]:=tem_2;
  617.             ply:=max_depth;
  618.             ply_minus_1:=ply-1;
  619.             IF ply_minus_1 > 0 THEN
  620.               BEGIN
  621.                 IF ODD(ply) THEN
  622.                   BEGIN
  623.                     change_index:=1;
  624.                     change_limit:=branch[ply_minus_1];
  625.                     skip_branch:=FALSE;
  626.                     WHILE ((change_index < change_limit)
  627.                     AND    (NOT skip_branch)) DO
  628.                       BEGIN
  629.                         tem_1:=min_max[ply_minus_1,change_index];
  630.                         IF tem_1 > -32000 THEN
  631.                           IF tem_2 >= tem_1 THEN
  632.                             skip_branch:=TRUE
  633.                           ELSE
  634.                             change_index:=change_index+1
  635.                         ELSE
  636.                           change_index:=change_index+1
  637.                       END;
  638.                     IF skip_branch THEN
  639.                       BEGIN
  640.                         change_limit:=branch[ply];
  641.                         FOR change_index:=1 TO change_limit DO
  642.                           min_max[ply,change_index]:=32000;
  643.                         branch[ply]:=1;
  644.                         ply:=ply_minus_1
  645.                       END
  646.                   END 
  647.                 ELSE
  648.                   BEGIN
  649.                     change_index:=1;
  650.                     change_limit:=branch[ply_minus_1];
  651.                     skip_branch:=FALSE;
  652.                     WHILE ((change_index < change_limit)
  653.                     AND    (NOT skip_branch)) DO
  654.                       BEGIN
  655.                         tem_1:=min_max[ply_minus_1,change_index];
  656.                         IF tem_1 < 32000 THEN
  657.                           IF tem_2 <= tem_1 THEN
  658.                             skip_branch:=TRUE
  659.                           ELSE
  660.                             change_index:=change_index+1
  661.                         ELSE
  662.                           change_index:=change_index+1
  663.                       END;
  664.                     IF skip_branch THEN
  665.                       BEGIN
  666.                         change_limit:=branch[ply];
  667.                         FOR change_index:=1 TO change_limit DO
  668.                           min_max[ply,change_index]:=-32000;
  669.                         branch[ply]:=1;
  670.                         ply:=ply_minus_1
  671.                       END
  672.                   END 
  673.               END
  674.           END 
  675.       UNTIL (ply = 0);
  676.       tem_1:=-32000;
  677.       FOR change_index:=1 to 8 DO
  678.         BEGIN
  679.           tem_2:=min_max[1,change_index];
  680.           IF tem_2 < 32000 THEN
  681.             BEGIN
  682.               IF tem_2 > tem_1 THEN
  683.                 BEGIN
  684.                   tem_1:=tem_2;
  685.                   final_change_index:=change_index
  686.                 END
  687.             END 
  688.         END;
  689.       next_column:=my_column[0]+column_change[final_change_index];
  690.       next_row:=my_row[0]+row_change[final_change_index];
  691.       my_column[0]:=next_column;
  692.       my_row[0]:=next_row;
  693.       tem_1:=board[0,next_column,next_row];
  694.       my_score:=my_score+tem_1;
  695.       IF tem_1 > 0 THEN
  696.         tiles_visited:=tiles_visited+1;
  697.       board[0,next_column,next_row]:=0;
  698.       plys_left:=plys_left-1
  699.     END;
  700.   BEGIN
  701.     ClrScr;
  702.     WRITELN(OUTPUT,'                                     Knight');
  703.     WRITELN(OUTPUT,' ');
  704.     WRITELN(OUTPUT,' ');
  705.     WRITELN(OUTPUT,' ');
  706.     display_rules;
  707.     your_row[0]:=0;
  708.     your_column[0]:=0;
  709.     my_row[0]:=4;
  710.     my_column[0]:=4;
  711.     tiles_visited:=2;
  712.     i_move_first:=NOT you_move_first;
  713.     get_level_of_difficulty;
  714.     max_plys:=level_of_difficulty;
  715.     get_game_number; 
  716.     set_up_board;
  717.     my_score:=0;
  718.     your_score:=0;
  719.     row_change[1]:=2; column_change[1]:=1;
  720.     row_change[2]:=1; column_change[2]:=2;
  721.     row_change[3]:=-1; column_change[3]:=2;
  722.     row_change[4]:=-2; column_change[4]:=1;
  723.     row_change[5]:=-2; column_change[5]:=-1;
  724.     row_change[6]:=-1; column_change[6]:=-2;
  725.     row_change[7]:=1; column_change[7]:=-2;
  726.     row_change[8]:=2; column_change[8]:=-1;
  727.     your_moves_left:=15;
  728.     plys_left:=2*your_moves_left;
  729.     IF i_move_first THEN
  730.       WHILE ((tiles_visited < 25)
  731.       AND    (plys_left > 0)) DO
  732.         BEGIN
  733.           display_board;
  734.           make_my_move;
  735.           IF tiles_visited < 25 THEN
  736.             BEGIN
  737.               display_board;
  738.               get_your_move
  739.             END
  740.         END
  741.     ELSE
  742.       WHILE ((tiles_visited < 25)
  743.       AND    (plys_left > 0)) DO
  744.         BEGIN
  745.           display_board;
  746.           get_your_move;
  747.           IF tiles_visited < 25 THEN
  748.             BEGIN
  749.               display_board;
  750.               make_my_move
  751.             END
  752.         END;
  753.     display_board;
  754.     WRITELN(OUTPUT,' ');
  755.     IF my_score > your_score THEN
  756.       WRITELN(OUTPUT,'     I win!')
  757.     ELSE
  758.       IF my_score < your_score THEN
  759.         WRITELN(OUTPUT,'     You win!')
  760.       ELSE
  761.         WRITELN(OUTPUT,'     The game is a draw.')
  762.   END.
  763.